﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RTUParameterConfig.Class;

namespace RTUParameterConfig
{
    public partial class CameraSetting : Form
    {
        public int intIndex;

        public CameraSetting(int index)
        {
            InitializeComponent();
            intIndex = index;
            cbbDj.Properties.Items.AddRange(Common.OpenStatus);
            cbbDj.SelectedIndex = 0;
            cbbCj.Properties.Items.AddRange(Common.CameraTimeInterval);
            cbbCj.SelectedIndex = 0;
            cbbHc.Properties.Items.AddRange(Common.OpenStatus);
            cbbHc.SelectedIndex = 0;
            cbbXy.Properties.Items.AddRange(Common.CameraPicAck);
            cbbXy.SelectedIndex = 0;
            chkTime.Items.AddRange(Common.CameraHours);
            this.cbbContent1.Properties.Items.AddRange(Common.VideoContentSelection);
            this.cbbContent2.Properties.Items.AddRange(Common.VideoContentSelection);
            this.cbbContent3.Properties.Items.AddRange(Common.VideoContentSelection);
            this.cbbContent4.Properties.Items.AddRange(Common.VideoContentSelection);
            this.cbbContent1.SelectedIndex = 0;
            this.cbbContent2.SelectedIndex = 0;
            this.cbbContent3.SelectedIndex = 0;
            this.cbbContent4.SelectedIndex = 0;
            SetValue();
        }

        /// <summary>
        /// 设置相机配置
        /// </summary>
        public void SetValue()
        {
            try
            {
                if (intIndex == 0)
                {
                    cbbDj.SelectedIndex = ParameterConfig.rtuConfigInfo.Camera_Overlying[0];
                    cbbCj.SelectedIndex = ParameterConfig.rtuConfigInfo.Camera_TimeInterval[0];
                    cbbHc.SelectedIndex = ParameterConfig.rtuConfigInfo.Camera_Cache[0];
                    cbbXy.SelectedIndex = ParameterConfig.rtuConfigInfo.Camera_PicAck[0];

                    for (int i = 0; i < 8; i++)
                    {
                        chkTime.SetItemChecked(i, Common.GetIntBit(ParameterConfig.rtuConfigInfo.Camera_Hours1[0], i) == 1 ? true : false);
                        chkTime.SetItemChecked(i + 8, Common.GetIntBit(ParameterConfig.rtuConfigInfo.Camera_Hours1[1], i) == 1 ? true : false);
                        chkTime.SetItemChecked(i + 16, Common.GetIntBit(ParameterConfig.rtuConfigInfo.Camera_Hours1[2], i) == 1 ? true : false);
                    }

                    txtContent1.Text = System.Text.Encoding.Default.GetString(ParameterConfig.rtuConfigInfo.VideoOverlayName11).Replace("\0", "");
                    txtContent2.Text = System.Text.Encoding.Default.GetString(ParameterConfig.rtuConfigInfo.VideoOverlayName12).Replace("\0", "");
                    txtContent3.Text = System.Text.Encoding.Default.GetString(ParameterConfig.rtuConfigInfo.VideoOverlayName13).Replace("\0", "");
                    txtContent4.Text = System.Text.Encoding.Default.GetString(ParameterConfig.rtuConfigInfo.VideoOverlayName14).Replace("\0", "");
                    cbbContent1.SelectedIndex = ParameterConfig.rtuConfigInfo.VideoContentSelection1[0];
                    cbbContent2.SelectedIndex = ParameterConfig.rtuConfigInfo.VideoContentSelection1[1];
                    cbbContent3.SelectedIndex = ParameterConfig.rtuConfigInfo.VideoContentSelection1[2];
                    cbbContent4.SelectedIndex = ParameterConfig.rtuConfigInfo.VideoContentSelection1[3];
                    nud1.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate11_X.Reverse<byte>().ToArray(), 0);
                    nud11.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate11_Y.Reverse<byte>().ToArray(), 0);
                    nud2.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate12_X.Reverse<byte>().ToArray(), 0);
                    nud21.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate12_Y.Reverse<byte>().ToArray(), 0);
                    nud3.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate13_X.Reverse<byte>().ToArray(), 0);
                    nud31.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate13_Y.Reverse<byte>().ToArray(), 0);
                    nud4.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate14_X.Reverse<byte>().ToArray(), 0);
                    nud41.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate14_Y.Reverse<byte>().ToArray(), 0);
                }

                if (intIndex == 1)
                {
                    cbbDj.SelectedIndex = ParameterConfig.rtuConfigInfo.Camera_Overlying[1];
                    cbbCj.SelectedIndex = ParameterConfig.rtuConfigInfo.Camera_TimeInterval[1];
                    cbbHc.SelectedIndex = ParameterConfig.rtuConfigInfo.Camera_Cache[1];
                    cbbXy.SelectedIndex = ParameterConfig.rtuConfigInfo.Camera_PicAck[1];

                    for (int i = 0; i < 8; i++)
                    {
                        chkTime.SetItemChecked(i, Common.GetIntBit(ParameterConfig.rtuConfigInfo.Camera_Hours2[0], i) == 1 ? true : false);
                        chkTime.SetItemChecked(i + 8, Common.GetIntBit(ParameterConfig.rtuConfigInfo.Camera_Hours2[1], i) == 1 ? true : false);
                        chkTime.SetItemChecked(i + 16, Common.GetIntBit(ParameterConfig.rtuConfigInfo.Camera_Hours2[2], i) == 1 ? true : false);
                    }

                    txtContent1.Text = System.Text.Encoding.Default.GetString(ParameterConfig.rtuConfigInfo.VideoOverlayName21).Replace("\0", "");
                    txtContent2.Text = System.Text.Encoding.Default.GetString(ParameterConfig.rtuConfigInfo.VideoOverlayName22).Replace("\0", "");
                    txtContent3.Text = System.Text.Encoding.Default.GetString(ParameterConfig.rtuConfigInfo.VideoOverlayName23).Replace("\0", "");
                    txtContent4.Text = System.Text.Encoding.Default.GetString(ParameterConfig.rtuConfigInfo.VideoOverlayName24).Replace("\0", "");
                    cbbContent1.SelectedIndex = ParameterConfig.rtuConfigInfo.VideoContentSelection2[0];
                    cbbContent2.SelectedIndex = ParameterConfig.rtuConfigInfo.VideoContentSelection2[1];
                    cbbContent3.SelectedIndex = ParameterConfig.rtuConfigInfo.VideoContentSelection2[2];
                    cbbContent4.SelectedIndex = ParameterConfig.rtuConfigInfo.VideoContentSelection2[3];
                    nud1.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate21_X.Reverse<byte>().ToArray(), 0);
                    nud11.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate21_Y.Reverse<byte>().ToArray(), 0);
                    nud2.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate22_X.Reverse<byte>().ToArray(), 0);
                    nud21.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate22_Y.Reverse<byte>().ToArray(), 0);
                    nud3.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate23_X.Reverse<byte>().ToArray(), 0);
                    nud31.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate23_Y.Reverse<byte>().ToArray(), 0);
                    nud4.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate24_X.Reverse<byte>().ToArray(), 0);
                    nud41.Value = BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.VideoContentCoordinate24_Y.Reverse<byte>().ToArray(), 0);
                }
            }
            catch
            {
                Common.ShowErrorMessage("加载高级设置参数失败！");
            }
        }

        /// <summary>
        /// 获取相机配置
        /// </summary>
        public void GetValue()
        {
            if (intIndex == 0)
            {
                ParameterConfig.rtuConfigInfo.Camera_Overlying[0] = Convert.ToByte(cbbDj.SelectedIndex);
                ParameterConfig.rtuConfigInfo.Camera_TimeInterval[0] = Convert.ToByte(cbbCj.SelectedIndex);
                ParameterConfig.rtuConfigInfo.Camera_Cache[0] = Convert.ToByte(cbbHc.SelectedIndex);
                ParameterConfig.rtuConfigInfo.Camera_PicAck[0] = Convert.ToByte(cbbXy.SelectedIndex);

                ParameterConfig.rtuConfigInfo.Camera_Hours1[0] = 0;
                ParameterConfig.rtuConfigInfo.Camera_Hours1[1] = 0;
                ParameterConfig.rtuConfigInfo.Camera_Hours1[2] = 0;
                for (int i = 0; i < chkTime.Items.Count; i++)
                {
                    if (chkTime.GetItemChecked(i))
                    {
                        if (i >= 0 && i < 8)
                        {
                            ParameterConfig.rtuConfigInfo.Camera_Hours1[0] |= (byte)(0x0001 << i);
                        }

                        if (i >= 8 && i < 16)
                        {
                            ParameterConfig.rtuConfigInfo.Camera_Hours1[1] |= (byte)(0x0001 << i % 8);
                        }

                        if (i >= 16 && i < 24)
                        {
                            ParameterConfig.rtuConfigInfo.Camera_Hours1[2] |= (byte)(0x0001 << i % 8);
                        }
                    }
                }

                ParameterConfig.rtuConfigInfo.VideoContentSelection1[0] = Convert.ToByte(cbbContent1.SelectedIndex);
                ParameterConfig.rtuConfigInfo.VideoContentSelection1[1] = Convert.ToByte(cbbContent2.SelectedIndex);
                ParameterConfig.rtuConfigInfo.VideoContentSelection1[2] = Convert.ToByte(cbbContent3.SelectedIndex);
                ParameterConfig.rtuConfigInfo.VideoContentSelection1[3] = Convert.ToByte(cbbContent4.SelectedIndex);
                ParameterConfig.rtuConfigInfo.VideoOverlayName11 = Common.GetByteForString(txtContent1.Text, 40);
                ParameterConfig.rtuConfigInfo.VideoOverlayName12 = Common.GetByteForString(txtContent2.Text, 40);
                ParameterConfig.rtuConfigInfo.VideoOverlayName13 = Common.GetByteForString(txtContent3.Text, 40);
                ParameterConfig.rtuConfigInfo.VideoOverlayName14 = Common.GetByteForString(txtContent4.Text, 40);
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate11_X = BitConverter.GetBytes(Convert.ToUInt16(nud1.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate11_Y = BitConverter.GetBytes(Convert.ToUInt16(nud11.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate12_X = BitConverter.GetBytes(Convert.ToUInt16(nud2.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate12_Y = BitConverter.GetBytes(Convert.ToUInt16(nud21.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate13_X = BitConverter.GetBytes(Convert.ToUInt16(nud3.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate13_Y = BitConverter.GetBytes(Convert.ToUInt16(nud31.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate14_X = BitConverter.GetBytes(Convert.ToUInt16(nud4.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate14_Y = BitConverter.GetBytes(Convert.ToUInt16(nud41.Value)).Reverse<byte>().ToArray();
            }

            if (intIndex == 1)
            {
                ParameterConfig.rtuConfigInfo.Camera_Overlying[1] = Convert.ToByte(cbbDj.SelectedIndex);
                ParameterConfig.rtuConfigInfo.Camera_TimeInterval[1] = Convert.ToByte(cbbCj.SelectedIndex);
                ParameterConfig.rtuConfigInfo.Camera_Cache[1] = Convert.ToByte(cbbHc.SelectedIndex);
                ParameterConfig.rtuConfigInfo.Camera_PicAck[1] = Convert.ToByte(cbbXy.SelectedIndex);

                ParameterConfig.rtuConfigInfo.Camera_Hours2[0] = 0;
                ParameterConfig.rtuConfigInfo.Camera_Hours2[1] = 0;
                ParameterConfig.rtuConfigInfo.Camera_Hours2[2] = 0;
                for (int i = 0; i < chkTime.Items.Count; i++)
                {
                    if (chkTime.GetItemChecked(i))
                    {
                        if (i >= 0 && i < 8)
                        {
                            ParameterConfig.rtuConfigInfo.Camera_Hours2[0] |= (byte)(0x0001 << i);
                        }

                        if (i >= 8 && i < 16)
                        {
                            ParameterConfig.rtuConfigInfo.Camera_Hours2[1] |= (byte)(0x0001 << i % 8);
                        }

                        if (i >= 16 && i < 24)
                        {
                            ParameterConfig.rtuConfigInfo.Camera_Hours2[2] |= (byte)(0x0001 << i % 8);
                        }
                    }
                }

                ParameterConfig.rtuConfigInfo.VideoContentSelection2[0] = Convert.ToByte(cbbContent1.SelectedIndex);
                ParameterConfig.rtuConfigInfo.VideoContentSelection2[1] = Convert.ToByte(cbbContent2.SelectedIndex);
                ParameterConfig.rtuConfigInfo.VideoContentSelection2[2] = Convert.ToByte(cbbContent3.SelectedIndex);
                ParameterConfig.rtuConfigInfo.VideoContentSelection2[3] = Convert.ToByte(cbbContent4.SelectedIndex);
                ParameterConfig.rtuConfigInfo.VideoOverlayName21 = Common.GetByteForString(txtContent1.Text, 40);
                ParameterConfig.rtuConfigInfo.VideoOverlayName22 = Common.GetByteForString(txtContent2.Text, 40);
                ParameterConfig.rtuConfigInfo.VideoOverlayName23 = Common.GetByteForString(txtContent3.Text, 40);
                ParameterConfig.rtuConfigInfo.VideoOverlayName24 = Common.GetByteForString(txtContent4.Text, 40);
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate21_X = BitConverter.GetBytes(Convert.ToUInt16(nud1.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate21_Y = BitConverter.GetBytes(Convert.ToUInt16(nud11.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate22_X = BitConverter.GetBytes(Convert.ToUInt16(nud2.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate22_Y = BitConverter.GetBytes(Convert.ToUInt16(nud21.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate23_X = BitConverter.GetBytes(Convert.ToUInt16(nud3.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate23_Y = BitConverter.GetBytes(Convert.ToUInt16(nud31.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate24_X = BitConverter.GetBytes(Convert.ToUInt16(nud4.Value)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.VideoContentCoordinate24_Y = BitConverter.GetBytes(Convert.ToUInt16(nud41.Value)).Reverse<byte>().ToArray();
            }
        }

        /// <summary>
        /// 关闭前保存修改的配置信息
        /// </summary>
        private void WaterLevelSetting_FormClosing(object sender, FormClosingEventArgs e)
        {
            GetValue();
            this.Dispose();
        }

        /// <summary>
        /// 全选
        /// </summary>
        private void linkAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            for (int i = 0; i < chkTime.Items.Count; i++)
            {
                chkTime.SetItemChecked(i, true);
            }
        }

        /// <summary>
        /// 清空选择项
        /// </summary>
        private void linkClear_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            for (int i = 0; i < chkTime.Items.Count; i++)
            {
                chkTime.SetItemChecked(i, false);
            }
        }
    }
}
